var express = require('express')
var router = express.Router()
const sqlQuery = require('../../module/sqlQuery')
const { resJsonObject } = require('../../module/canshu')

/**
 * 返回所有分类类别数据
 */
async function getCategoryList() {
  const results = await sqlQuery('select * from news_category')
  return results
}

/* GET users listing. */
router.get('/', function (req, res, next) {
  res.render('admin/statistics/echarts1')
})

router.get('/data', async function (req, res, next) {
  try {
    // keywords 搜索关键词 为空的时候 查询全部
    // let { page, limit, keywords_title = '' } = req.query
    // console.log(req.query)

    // page = parseInt(page ? page : 1)
    // limit = parseInt(limit)
    const results = await sqlQuery(
      `SELECT doctors_category.name as doctors_category_name ,count(doctors_category.name) as category_num
      FROM registration_record as re 
      LEFT JOIN doctors_category
      ON re.clinic_id=doctors_category.id
			GROUP BY(doctors_category.name)`
    )

    res.json(
      resJsonObject({
        code: 0,
        msg: '完成',
        data: {
          arr: Array.from(results),
        },
      })
    )
    // res.render('admin/users/userlist',{userlist:results});
  } catch (error) {
    console.log(error)
    res.json(
      resJsonObject({
        code: 1,
        msg: '获取失败',
        data: {
          error,
        },
      })
    )
  }
})

module.exports = router
